package cn.fbs.controller;

import cn.fbs.dao.dm.UserGroupMapper;
import cn.fbs.dao.dm.UserTargetMapper;
import cn.fbs.model.NewTcodeTargetTree;
import cn.fbs.service.JointTrialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

@Api(tags = "会审")
@RestController
@RequestMapping("trial")
public class JointTrialController {

    @Autowired
    private JointTrialService jointTrialService;

    @Autowired
    private UserGroupMapper  userGroupMapper;

    @Autowired
    private UserTargetMapper userTargetMapper;

//旧接口复用
//    @ApiOperation("获得所有成果数据")
//    @RequestMapping("getAllResultData")
//    @ApiImplicitParam(name = "mediaType",value = "媒体类型,1资料,2图像,3视频")
//    public  Object getAllResultData(int mediaType,int pageNo,int pageSize){
//       return jointTrialService.getAllResultData(mediaType,pageNo,pageSize);
//    }


    @ApiOperation("获得可以审核的目标")
    @GetMapping("getMyTarget")
    public  Object getMyTarget(@RequestParam("userId") Long userId){
//        HttpSession session = request.getSession();
//        Long userId = (Long) session.getAttribute("userId");
        List<String> targets=userGroupMapper.getTargetByUserId(userId);
        List<NewTcodeTargetTree> topNodes=jointTrialService.getMyTarget(targets);
        return topNodes;
    }

    @ApiOperation("获得小组内为用户分配的目标")
    @GetMapping("getUserTarget")
    public  Object getUserTarget(@RequestParam("userId") Long userId){
//        HttpSession session = request.getSession();
//        Long userId = (Long) session.getAttribute("userId");
//        List<String> targets=userGroupMapper.getTargetByUserId(userId);
        List<String> targets=userTargetMapper.getTargetByUserId(userId);

//        List<NewTcodeTargetTree> topNodes=jointTrialService.getMyTarget(targets);
        return targets;
    }

    @ApiOperation("获得小组下的目标")
    @GetMapping("getGroupTarget")
    public  Object getGroupTarget(Long groupId){
        List<String> targets=userGroupMapper.getTargetByGroupId(groupId);
        List<NewTcodeTargetTree> topNodes=jointTrialService.getMyTarget(targets);
        return topNodes;
    }



    private void filterTarget(List<NewTcodeTargetTree> targetTrees) {
        Iterator<NewTcodeTargetTree> iterator = targetTrees.iterator();

        while (iterator.hasNext()){
            NewTcodeTargetTree targetTree = iterator.next();
            if (targetTree.isBelongMe()){
                continue;
            }

        }

    }



    private List<NewTcodeTargetTree> giveFlatBit(List<NewTcodeTargetTree> categoryTrees, List<Long> targets) {
        for (Long target : targets) {
            for (NewTcodeTargetTree targetTree : categoryTrees) {
                if (targetTree.getXh().equals(target.toString())){
                    targetTree.setBelongMe(true);
                }
                List<NewTcodeTargetTree> childrenTarget = targetTree.getChildrenTarget();
                if (childrenTarget!=null){
                    giveFlatBit(childrenTarget,targets);
                }
            }
        }
        return categoryTrees;
    }

    //分页
    @ApiOperation("获得可以审核的成果数据")
    @GetMapping("getMyResultData")
    public  Object getMyResultData(int targetId){
        return null;
    }







}
